package com.springboot.sitework.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.springboot.biz.ItemBiz;
import com.springboot.biz.OperatorBiz;
import com.springboot.biz.PipeBiz;
import com.springboot.biz.ProjectBiz;
import com.springboot.entity.Pipe;
import com.springboot.entity.Project;
import com.springboot.entity.User;
import com.springboot.util.AppHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController(value = "siteworkProjectController")
@RequestMapping(value = "/sitework/project")
public class ProjectController {

    @Resource
    private OperatorBiz operatorBiz;
    @Resource
    private ProjectBiz projectBiz;
    @Resource
    private PipeBiz pipeBiz;
    private Map<String, Object> map = null;

    @RequestMapping(value = "/showlist")
    public ModelAndView showList(@RequestParam(defaultValue = "1") int page) {
        ModelAndView view = new ModelAndView("/sitework/showlist");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        map = AppHelper.getMap("state", "待提交", "user", user, "page", page);
        PageInfo<Project> info = projectBiz.findListProject(map);
        view.addObject("projects", info.getList());
        view.addObject("count", info.getPages());
        view.addObject("page", page);
        return view;
    }

    @RequestMapping(value = "/findinfo")
    public ModelAndView findInfo(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (StringUtils.isEmpty(project))
            return view;
        List<Pipe> pipes = pipeBiz.findListPipe(project);
        pipes.sort((p1, p2) -> {
            int compare = StrUtil.compare(p2.getDate(), p1.getDate(), true);
            return compare == 0 ? StrUtil.compare(p1.getNo() + "", p2.getNo() + "", true) : compare;
        });
        view.setViewName("/sitework/findinfo");
        view.addObject("project", project);
        view.addObject("pipes", pipes);
        return view;
    }

    @RequestMapping(value = "/insertview")
    public ModelAndView insertView() {
        ModelAndView view = new ModelAndView("/sitework/insert");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        List<String> names = operatorBiz.findListName(user.getCompany());
        view.addObject("names", names);
        return view;
    }


    @RequestMapping(value = "/insert")
    public ModelAndView insert(Project project) {
        ModelAndView view = new ModelAndView();
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        project.setUser(user);
        projectBiz.insertProject(project);
        view.setViewName("redirect:findinfo?id=" + project.getId());
        return view;
    }

    @RequestMapping(value = "/updateview")
    public ModelAndView updateView(@RequestParam(defaultValue = "0") int id) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (StringUtils.isEmpty(project))
            return view;
        List<String> names = operatorBiz.findListName(user.getCompany());
        view.setViewName("sitework/update");
        view.addObject("project", project);
        view.addObject("names", names);
        return view;
    }

    @RequestMapping(value = "/update")
    public ModelAndView update(Project project) {
        ModelAndView view = new ModelAndView("userview/failure");
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        map = AppHelper.getMap("id", project.getId(), "user", user);
        if (projectBiz.findInfoProject(map) == null)
            return view;
        project.setUser(user);
        projectBiz.updateProject(project);
        view.setViewName("redirect:findinfo?id=" + project.getId());
        return view;
    }

    /**
     * 提交数据
     */
    @Transactional
    @RequestMapping(value = "/submit")
    public boolean submit(@RequestParam(defaultValue = "0") int id) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (Objects.nonNull(project)) {
            project.setState("待完成");
            project.setCommit("N");
            project.setUser(user);
            projectBiz.updateProject(project);
        }
        return true;
    }

    /**
     * 删除项目
     */
    @RequestMapping(value = "/delete")
    public boolean delete(@RequestParam(defaultValue = "0") int id) {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("user");
        Project project = projectBiz.findInfoProject(id, user);
        if (!StringUtils.isEmpty(project))
            projectBiz.deleteProject(project);
        return true;
    }

}
